package com.rfid.project.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.ProjectStakeholderType;
import com.rfid.project.service.ProjectStakeholderTypeService;
import com.rfid.project.util.StringUtil;

@Controller
public class ProjectStakeholderTypeController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProjectStakeholderTypeController.class);

	@Autowired
	private ProjectStakeholderTypeService projectStakeholderTypeService;

	@RequestMapping(value = "/projectStakeholderTypemgr")
	public String projectStakeholderTypemgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "projectStakeholderTypemgr";
	}

	@RequestMapping(value = "/queryProjectStakeholderType")
	public String queryProjectStakeholderType(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectStakeholderType> items = projectStakeholderTypeService
				.findAll(name, remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", items.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectStakeholderType authority : items) {
			JSONObject mod = new JSONObject();
			wrapData(authority, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(ProjectStakeholderType authority, JSONObject mod) {
		mod.put("id", authority.getId());
		mod.put("name", authority.getName());
		mod.put("orderNumber", authority.getOrderNumber());
		mod.put("remark", StringUtil.getString(authority.getRemark()));
	}
	
	@RequestMapping(value = "/findProjectStakeholderType")
	public String findProjectStakeholderType(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model) {
		ProjectStakeholderType projectStakeholderType = projectStakeholderTypeService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(projectStakeholderType, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	@RequestMapping(value = "/editProjectStakeholderType", method = RequestMethod.POST)
	public String editAuthority(HttpServletRequest request,
			HttpServletResponse response, @Valid ProjectStakeholderType authority,
			Model model, RedirectAttributes redirectAttributes) {
		projectStakeholderTypeService.saveProjectStakeholderType(authority);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delProjectStakeholderType")
	public String delProjectStakeholderType(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.projectStakeholderTypeService.deleteProjectStakeholderType(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/projectStakeholderTypeChangeOrderNumber")
	public String changeOrderNumber(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			@RequestParam(value = "targetId", defaultValue = "") Long targetId){
		JSONObject result = new JSONObject();
		boolean flag = this.projectStakeholderTypeService.changeOrderNumber(id, targetId);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	

}
